package com.example.wlzz.controller;

import com.example.wlzz.constant.RedisConstant;
import com.example.wlzz.dto.SelectUserInfoDTO;
import com.example.wlzz.dto.UserInfoDTO;
import com.example.wlzz.entity.UserInfo;
import com.example.wlzz.entity.User_;
import com.example.wlzz.enums.ResultEnum;
import com.example.wlzz.repository.UserInfoRepository;
import com.example.wlzz.service.UserInfoService;
import com.example.wlzz.service.UserService;
import com.example.wlzz.service.UserWithFriendsService;
import com.example.wlzz.utils.ResultVOUtil;
import com.example.wlzz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/wlzzFriends")
@Slf4j
@CrossOrigin
@Api(tags = "好友模块")
public class FriendController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoRepository userInfoRepository;  //直接注入，没啥逻辑不写service
    @Autowired
    private UserWithFriendsService userWithFriendsService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("/selectFriendsForPhone")
    @ApiOperation("通过手机号码查询好友")
    public ResultVO selectFriendsForPhone(@ApiParam("好友手机号码") @RequestParam("phone") String phone,
                                          @ApiParam("token") @RequestParam("token") String token){
        User_ user = userService.findByPhone(phone);
        if(user==null){
            log.warn("【查询好友】通过手机号码查询好友不存在，phone={}",phone);
            return ResultVOUtil.error(ResultEnum.USER_NO_EXIST.getCode(),ResultEnum.USER_NO_EXIST.getMessage());
        }
        UserInfo userInfo = userInfoService.findByUserId(user.getUserId());
        if(userInfo==null){   //一般这种情况不可能
            log.warn("【查询好友】通过手机号码查询好友个人信息不存在，phone={}",phone);
            return ResultVOUtil.error(ResultEnum.USERINFO_NO_EXIST.getCode(),ResultEnum.USERINFO_NO_EXIST.getMessage());
        }
        log.info("查询该好友信息：userInfo={}",userInfo);
        SelectUserInfoDTO selectUserInfoDTO = new SelectUserInfoDTO();
        BeanUtils.copyProperties(userInfo,selectUserInfoDTO); //虽减少的代码量，但是耗时长。
        return ResultVOUtil.success(selectUserInfoDTO);
    }
    @PostMapping("/selectFriendsForSno")
    @ApiOperation("通过学号查询好友")
    public ResultVO selectFriendsForSno(@ApiParam("好友学号") @RequestParam("sno") String sno,
                                          @ApiParam("token") @RequestParam("token") String token){
        List<UserInfo> userInfo = userInfoRepository.findBySno(sno);
        if(userInfo.size()<=0){
            log.warn("【查询好友】通过学号查询好友不存在，sno={}",sno);
            return ResultVOUtil.error(ResultEnum.SNO_NO_EXIST.getCode(),ResultEnum.SNO_NO_EXIST.getMessage());
        }
        List<SelectUserInfoDTO> selectUserInfoDTOList = new ArrayList<>();
        for (UserInfo userInfo1 : userInfo) {
            SelectUserInfoDTO selectUserInfoDTO = new SelectUserInfoDTO();
            BeanUtils.copyProperties(userInfo1,selectUserInfoDTO);
            selectUserInfoDTOList.add(selectUserInfoDTO);
        }
        return ResultVOUtil.success(selectUserInfoDTOList);
    }

    @PostMapping("/applyForFriends")
    @ApiOperation("申请添加好友")
    public ResultVO applyForFriends(@ApiParam("想要添加好友的userid") @RequestParam("toUserid") int toUserid,
                                        @ApiParam("token") @RequestParam("token") String token){
            ResultVO resultVO = userInfoService.getUserInfo(token);
            UserInfoDTO userInfo = (UserInfoDTO)resultVO.getData();
            return  userWithFriendsService.applyForFriends(userInfo.getUserId(),toUserid);
    }
    @PostMapping("/getApplyedList")
    @ApiOperation("获取申请好友列表")
    public ResultVO getApplyedList(@ApiParam("token") @RequestParam("token") String token){
        ResultVO resultVO = userInfoService.getUserInfo(token);
        UserInfoDTO userInfo = (UserInfoDTO)resultVO.getData();
        return userWithFriendsService.getApplyedList(userInfo.getUserId());
    }
    @PostMapping("/agreeForFriend")
    @ApiOperation("同意添加好友")
    public ResultVO agreeForFriend(@ApiParam("token") @RequestParam("token") String token,
                                   @ApiParam("申请列表的id") @RequestParam("_id") String _id){
        //去redis查询
        String phoneToken = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        User_ user = userService.findByPhone(phoneToken);
        return userWithFriendsService.agreeApply(_id,user.getUserId());
    }
    @PostMapping("/rejectForFriend")
    @ApiOperation("拒绝添加好友")
    public ResultVO rejectForFriend(@ApiParam("token") @RequestParam("token") String token,
                                   @ApiParam("申请列表的id") @RequestParam("_id") String _id){
        //去redis查询
        String phoneToken = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        User_ user = userService.findByPhone(phoneToken);
        return userWithFriendsService.rejectApply(_id,user.getUserId());
    }
    @PostMapping("/getFriends")
    @ApiOperation("获取好友列表")
    public ResultVO getFriends(@ApiParam("token") @RequestParam("token") String token)
    {
        //去redis查询
        String phoneToken = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        User_ user = userService.findByPhone(phoneToken);
        return userWithFriendsService.getFriends(user.getUserId());
    }
}
